﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Data.SqlClient;
using System.Data;

namespace Restaurant.DAO
{
    public class NhanVienDB
    {
        private SqlConnection conn = KetNoiDB.TaoKetNoi();

        public DataTable LayNhanVien(string username, string pass)
        {
            try
            {
                
                conn.Open();
                string strCmd = "dbo.spLayNhanVien";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@username", username));
                cmd.Parameters.Add(new SqlParameter("@pass", pass));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return dbtab;
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }

        public DataTable LayTatCaLoaiNV()
        {
            try
            {
                
                conn.Open();
                string strCmd = "dbo.spLayTatCaLoaiNV";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return dbtab;
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        public DataTable LayTatCaNV()
        {
            try
            {
                
                conn.Open();
                string strCmd = "dbo.spTimTatCaNV";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return dbtab;
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        public DataTable LayLoaiNV(int ma)
        {
            try
            {
                
                conn.Open();
                string strCmd = "dbo.spLayLoaiNV";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maloai", ma));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return dbtab;
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        public DataTable LayMaLoaiNV(string tenloai)
        {
            try
            {

                conn.Open();
                string strCmd = "dbo.spLayMaLoaiNV";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@tenloai", tenloai));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return dbtab;
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        public DataTable LayNVTheoLoai(int maloai)
        {
            try
            {

                conn.Open();
                string strCmd = "dbo.spLayNVTheoLoai";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@ma", maloai));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return dbtab;
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        public DataTable TimNVTheoTen(string ten)
        {
            try
            {

                conn.Open();
                string strCmd = "dbo.spTimTenNV";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@tenNV", ten));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return dbtab;
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        public bool ThemNV(string ten, string cmnd, string diachi, string sdt, DateTime ngaysinh, int maloainv, int manh, DateTime ngaylam, string gioitinh)
        {
            try
            {
                conn.Open();
                string strcmd = "dbo.spThemNhanVien";
                SqlCommand cmd = new SqlCommand(strcmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@tenNV", ten));
                cmd.Parameters.Add(new SqlParameter("@cmnd", cmnd));
                cmd.Parameters.Add(new SqlParameter("@diaChi", diachi));
                cmd.Parameters.Add(new SqlParameter("@sdt", sdt));
                cmd.Parameters.Add(new SqlParameter("@ngaySinh", ngaysinh));
                cmd.Parameters.Add(new SqlParameter("@maLoaiNV", maloainv));
                cmd.Parameters.Add(new SqlParameter("@maNH", manh));
                cmd.Parameters.Add(new SqlParameter("@ngayLam", ngaylam));
                cmd.Parameters.Add(new SqlParameter("@gioitinh", gioitinh));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtb = new DataTable();
                adapt.Fill(dbtb);
                conn.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool SuaNV(int manv, string ten, string cmnd, string diachi, string sdt, DateTime ngaysinh, int maloainv, int manh, DateTime ngaylam, string gioitinh,string username, string password)
        {
            try
            {
                conn.Open();
                string strcmd = "dbo.spCapNhatNhanVien";
                SqlCommand cmd = new SqlCommand(strcmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maNV", manv));
                cmd.Parameters.Add(new SqlParameter("@tenNV", ten));
                cmd.Parameters.Add(new SqlParameter("@cmnd", cmnd));
                cmd.Parameters.Add(new SqlParameter("@diaChi", diachi));
                cmd.Parameters.Add(new SqlParameter("@sdt", sdt));
                cmd.Parameters.Add(new SqlParameter("@ngaySinh", ngaysinh));
                cmd.Parameters.Add(new SqlParameter("@maLoaiNV", maloainv));
                cmd.Parameters.Add(new SqlParameter("@maNH", manh));
                cmd.Parameters.Add(new SqlParameter("@ngayLam", ngaylam));
                cmd.Parameters.Add(new SqlParameter("@gioitinh", gioitinh));
                cmd.Parameters.Add(new SqlParameter("@username", username));
                cmd.Parameters.Add(new SqlParameter("@password", password));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtb = new DataTable();
                adapt.Fill(dbtb);
                conn.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool XoaNV(int ma)
        {
            try
            {
                conn.Open();
                string strcmd = "dbo.spXoaNV";
                SqlCommand cmd = new SqlCommand(strcmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@ma", ma));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtb = new DataTable();
                adapt.Fill(dbtb);
                conn.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public DataTable TimNVTheoID(int ma)
        {
            try
            {
                conn.Open();
                string strcmd = "dbo.spTimNVID";
                SqlCommand cmd = new SqlCommand(strcmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@ma", ma));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtb = new DataTable();
                adapt.Fill(dbtb);
                conn.Close();
                return dbtb;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable LoadNVDirtyreadError()
        {
            try
            {

                conn.Open();
                string strCmd = "dbo.spDirtyRead_TimNV_Error";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return dbtab;
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        public DataTable LoadNVDirtyread()
        {
            try
            {

                conn.Open();
                string strCmd = "dbo.spDirtyRead_TimNV_Fixed";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return dbtab;
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        public bool CapNhatNVDirtyreadError(int manv, string ten, string cmnd, string diachi, string sdt, DateTime ngaysinh, int maloainv, int manh, DateTime ngaylam, string gioitinh)
        {
            try
            {
                conn.Open();
                string strcmd = "dbo.spDirtyRead_SuaNV_Error";
                SqlCommand cmd = new SqlCommand(strcmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maNV", manv));
                cmd.Parameters.Add(new SqlParameter("@tenNV", ten));
                cmd.Parameters.Add(new SqlParameter("@cmnd", cmnd));
                cmd.Parameters.Add(new SqlParameter("@diaChi", diachi));
                cmd.Parameters.Add(new SqlParameter("@sdt", sdt));
                cmd.Parameters.Add(new SqlParameter("@ngaySinh", ngaysinh));
                cmd.Parameters.Add(new SqlParameter("@maLoaiNV", maloainv));
                cmd.Parameters.Add(new SqlParameter("@maNH", manh));
                cmd.Parameters.Add(new SqlParameter("@ngayLam", ngaylam));
                cmd.Parameters.Add(new SqlParameter("@gioitinh", gioitinh));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtb = new DataTable();
                adapt.Fill(dbtb);
                conn.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool CapNhatNVDirtyread(int manv, string ten, string cmnd, string diachi, string sdt, DateTime ngaysinh, int maloainv, int manh, DateTime ngaylam, string gioitinh)
        {
            try
            {
                conn.Open();
                string strcmd = "dbo.spDirtyRead_SuaNV_Fixed";
                SqlCommand cmd = new SqlCommand(strcmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maNV", manv));
                cmd.Parameters.Add(new SqlParameter("@tenNV", ten));
                cmd.Parameters.Add(new SqlParameter("@cmnd", cmnd));
                cmd.Parameters.Add(new SqlParameter("@diaChi", diachi));
                cmd.Parameters.Add(new SqlParameter("@sdt", sdt));
                cmd.Parameters.Add(new SqlParameter("@ngaySinh", ngaysinh));
                cmd.Parameters.Add(new SqlParameter("@maLoaiNV", maloainv));
                cmd.Parameters.Add(new SqlParameter("@maNH", manh));
                cmd.Parameters.Add(new SqlParameter("@ngayLam", ngaylam));
                cmd.Parameters.Add(new SqlParameter("@gioitinh", gioitinh));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtb = new DataTable();
                adapt.Fill(dbtb);
                conn.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public DataTable LoadNVUnrepeatError(ref int soluong,ref int nam,ref int nu)
        {
            conn.Open();
            string strCmd = "dbo.spUnrepeatable_TimNV_Error";
            SqlCommand cmd = new SqlCommand(strCmd, conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@soluong", soluong));
            cmd.Parameters.Add(new SqlParameter("@nam", nam));
            cmd.Parameters.Add(new SqlParameter("@nu", nu));
            cmd.Parameters["@soluong"].Direction = ParameterDirection.Output;
            cmd.Parameters["@nam"].Direction = ParameterDirection.Output;
            cmd.Parameters["@nu"].Direction = ParameterDirection.Output;
            SqlDataAdapter adapt = new SqlDataAdapter(cmd);
            DataTable dbtab = new DataTable();
            adapt.Fill(dbtab);
            soluong = int.Parse(cmd.Parameters["@soluong"].Value.ToString());
            nam = int.Parse(cmd.Parameters["@nam"].Value.ToString());
            nu = int.Parse(cmd.Parameters["@nu"].Value.ToString());
            conn.Close();
            return dbtab;
        }

        public DataTable LoadNVUnrepeat(ref int soluong, ref int nam, ref int nu)
        {
            conn.Open();
            string strCmd = "dbo.spUnrepeatable_TimNV_Fixed";
            SqlCommand cmd = new SqlCommand(strCmd, conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@soluong", soluong));
            cmd.Parameters.Add(new SqlParameter("@nam", nam));
            cmd.Parameters.Add(new SqlParameter("@nu", nu));
            cmd.Parameters["@soluong"].Direction = ParameterDirection.Output;
            cmd.Parameters["@nam"].Direction = ParameterDirection.Output;
            cmd.Parameters["@nu"].Direction = ParameterDirection.Output;
            SqlDataAdapter adapt = new SqlDataAdapter(cmd);
            DataTable dbtab = new DataTable();
            adapt.Fill(dbtab);
            soluong = int.Parse(cmd.Parameters["@soluong"].Value.ToString());
            nam = int.Parse(cmd.Parameters["@nam"].Value.ToString());
            nu = int.Parse(cmd.Parameters["@nu"].Value.ToString());
            conn.Close();
            return dbtab;
        }

        public DataTable LoadNVPhantomError(ref int soluong, ref int nam, ref int nu)
        {
            conn.Open();
            string strCmd = "dbo.spPhantom_TimNV_Error";
            SqlCommand cmd = new SqlCommand(strCmd, conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@soluong", soluong));
            cmd.Parameters.Add(new SqlParameter("@nam", nam));
            cmd.Parameters.Add(new SqlParameter("@nu", nu));
            cmd.Parameters["@soluong"].Direction = ParameterDirection.Output;
            cmd.Parameters["@nam"].Direction = ParameterDirection.Output;
            cmd.Parameters["@nu"].Direction = ParameterDirection.Output;
            SqlDataAdapter adapt = new SqlDataAdapter(cmd);
            DataTable dbtab = new DataTable();
            adapt.Fill(dbtab);
            soluong = int.Parse(cmd.Parameters["@soluong"].Value.ToString());
            nam = int.Parse(cmd.Parameters["@nam"].Value.ToString());
            nu = int.Parse(cmd.Parameters["@nu"].Value.ToString());
            conn.Close();
            return dbtab;
        }

        public DataTable LoadNVPhantom(ref int soluong, ref int nam, ref int nu)
        {
            conn.Open();
            string strCmd = "dbo.spPhantom_TimNV_Fixed";
            SqlCommand cmd = new SqlCommand(strCmd, conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@soluong", soluong));
            cmd.Parameters.Add(new SqlParameter("@nam", nam));
            cmd.Parameters.Add(new SqlParameter("@nu", nu));
            cmd.Parameters["@soluong"].Direction = ParameterDirection.Output;
            cmd.Parameters["@nam"].Direction = ParameterDirection.Output;
            cmd.Parameters["@nu"].Direction = ParameterDirection.Output;
            SqlDataAdapter adapt = new SqlDataAdapter(cmd);
            DataTable dbtab = new DataTable();
            adapt.Fill(dbtab);
            soluong = int.Parse(cmd.Parameters["@soluong"].Value.ToString());
            nam = int.Parse(cmd.Parameters["@nam"].Value.ToString());
            nu = int.Parse(cmd.Parameters["@nu"].Value.ToString());
            conn.Close();
            return dbtab;
        }

        public bool CapNhatNVLostUpdateError(int manv, string ten, string cmnd, string diachi, string sdt, DateTime ngaysinh, int maloainv, int manh, DateTime ngaylam, string gioitinh, string username, string password)
        {
            try
            {
                conn.Open();
                string strcmd = "dbo.spLostUpdate_SuaNV_Error";
                SqlCommand cmd = new SqlCommand(strcmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maNV", manv));
                cmd.Parameters.Add(new SqlParameter("@tenNV", ten));
                cmd.Parameters.Add(new SqlParameter("@cmnd", cmnd));
                cmd.Parameters.Add(new SqlParameter("@diaChi", diachi));
                cmd.Parameters.Add(new SqlParameter("@sdt", sdt));
                cmd.Parameters.Add(new SqlParameter("@ngaySinh", ngaysinh));
                cmd.Parameters.Add(new SqlParameter("@maLoaiNV", maloainv));
                cmd.Parameters.Add(new SqlParameter("@maNH", manh));
                cmd.Parameters.Add(new SqlParameter("@ngayLam", ngaylam));
                cmd.Parameters.Add(new SqlParameter("@gioitinh", gioitinh));
                cmd.Parameters.Add(new SqlParameter("@username", username));
                cmd.Parameters.Add(new SqlParameter("@password", password));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtb = new DataTable();
                adapt.Fill(dbtb);
                conn.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool CapNhatNVLostUpdate(int manv, string ten, string cmnd, string diachi, string sdt, DateTime ngaysinh, int maloainv, int manh, DateTime ngaylam, string gioitinh, string username, string password)
        {
            try
            {
                conn.Open();
                string strcmd = "dbo.spLostUpdate_SuaNV_Fixed";
                SqlCommand cmd = new SqlCommand(strcmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maNV", manv));
                cmd.Parameters.Add(new SqlParameter("@tenNV", ten));
                cmd.Parameters.Add(new SqlParameter("@cmnd", cmnd));
                cmd.Parameters.Add(new SqlParameter("@diaChi", diachi));
                cmd.Parameters.Add(new SqlParameter("@sdt", sdt));
                cmd.Parameters.Add(new SqlParameter("@ngaySinh", ngaysinh));
                cmd.Parameters.Add(new SqlParameter("@maLoaiNV", maloainv));
                cmd.Parameters.Add(new SqlParameter("@maNH", manh));
                cmd.Parameters.Add(new SqlParameter("@ngayLam", ngaylam));
                cmd.Parameters.Add(new SqlParameter("@gioitinh", gioitinh));
                cmd.Parameters.Add(new SqlParameter("@username", username));
                cmd.Parameters.Add(new SqlParameter("@password", password));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtb = new DataTable();
                adapt.Fill(dbtb);
                conn.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}
